package com.elong.qq.http;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * QQHttpCookie
  * @Title: QQHttpCookie
  * @Desription 
  * @author Guanguo.Gao
  * @date 2015年1月13日 上午8:53:26
 */
public class QQHttpCookie {
    
    private String name;
    
    private String value;
    
    private String domin;
    
    private String path;
    
    private Date expireDate;

    /** Constructor for QQHttpCookie. 
      * <p>Title: </p>
      * <p>Description: </p>
      * @param name
      * @param value
      * @param domin
      * @param path
      * @param expireDate
      */
    
    public QQHttpCookie(String name, String value, String domin, String path,
	    Date expireDate) {
	super();
	this.name = name;
	this.value = value;
	this.domin = domin;
	this.path = path;
	this.expireDate = expireDate;
    }
    
    
    /**
     * cookie格式解析
     */
    public QQHttpCookie(String cookie){
   	/*
    	这里只解析name, value, domain, path, Max-Age(expired)
    	The syntax for the Set-Cookie response header is

    	   set-cookie      =       "Set-Cookie:" cookies
    	   cookies         =       1#cookie
    	   cookie          =       NAME "=" VALUE *(";" cookie-av)
    	   NAME            =       attr
    	   VALUE           =       value
    	   cookie-av       =       "Comment" "=" value
    	                   |       "Domain" "=" value
    	                   |       "Max-Age" "=" value
    	                   |       "Path" "=" value
    	                   |       "Secure"
    	                   |       "Version" "=" 1*DIGIT
		*/
	String[] parts = cookie.split(";");
	if(parts.length < 2){
	    throw new IllegalArgumentException("Invalid cookie string: " + cookie);
	}
	for (int i = 0; i < parts.length; i++) {
	    String[] pairs = parts[i].split("=");
	    String key = pairs[0].trim();
	    String val = pairs.length > 1 ? pairs[1].trim() : "";
	    if(i == 0){
		this.name = key;
		this.value = val;
	    }
	    else{
		key = key.toLowerCase();
		if(key.equals("domin")){
		    this.domin = val;
		}else if(key.equals("max-age")){
		    try {
			expireDate = SimpleDateFormat.getInstance().parse(val);
		    } catch (Exception e) {
			throw new IllegalArgumentException("parse exipred failed.", e);
		    }
		}else if(key.equals("path")){
		    this.path = val;
		}
	    }
	}
    }


    /**
     * getter method for name
     * @return the name
     */
    public String getName() {
        return name;
    }


    /**
     * setter method for name
     * @Description the name to set
     * @param name 
     */
    public void setName(String name) {
        this.name = name;
    }


    /**
     * getter method for value
     * @return the value
     */
    public String getValue() {
        return value;
    }


    /**
     * setter method for value
     * @Description the value to set
     * @param value 
     */
    public void setValue(String value) {
        this.value = value;
    }


    /**
     * getter method for domin
     * @return the domin
     */
    public String getDomin() {
        return domin;
    }


    /**
     * setter method for domin
     * @Description the domin to set
     * @param domin 
     */
    public void setDomin(String domin) {
        this.domin = domin;
    }


    /**
     * getter method for path
     * @return the path
     */
    public String getPath() {
        return path;
    }


    /**
     * setter method for path
     * @Description the path to set
     * @param path 
     */
    public void setPath(String path) {
        this.path = path;
    }


    /**
     * getter method for expireDate
     * @return the expireDate
     */
    public Date getExpireDate() {
        return expireDate;
    }


    /**
     * setter method for expireDate
     * @Description the expireDate to set
     * @param expireDate 
     */
    public void setExpireDate(Date expireDate) {
        this.expireDate = expireDate;
    }
    
}
